<template>
	<view class="container">
		<view class="list">
			<view class="item" v-for="(item,i) in addressList" :key="item.id">
				<view class="left" @click="isClick ? selectAddress(item) : ''">
					<view class="city">
						<text v-if="item.default=='1'">默认</text>
						<view class="text">
							{{item.formatted_address.split('/').join(' ')}}
						</view>
					</view>
					<view class="info">
						{{item.address}}
					</view>
					<view class="user">
						{{item.name}} {{item.mobile}}
					</view>
				</view>
				<image :src="$img_path('/pagesMine/65.png')" @click="toForm(2,item)" mode="widthFix"></image>
			</view>
		</view>
		<view class="add-btn">
			<button @click="toForm(1)">新增地址</button>
		</view>
	</view>
</template>

<script>
	import { getaddress } from '@/utils/api/mallApi.js'
	export default {
		data() {
			return {
				isClick:false,
				addressList:[],
				page:1,
				total:0,
				last_page:0,
			};
		},
		onLoad(options) {
			if(options.prev){
				this.isClick = true
			}
		},
		onShow() {
			this.getaddress()
		},
		methods:{
			selectAddress(data){
				uni.$emit('checkAddress',data)
				uni.navigateBack()
			},
			
			getaddress(){
				uni.showLoading({})
				getaddress({page:this.page}).then(res=>{
					if(this.page==1){
						this.addressList = res.data.data
					}else{
						this.addressList = [...this.addressList,...res.data.data]
					}
					
					
					this.total = res.data.total
					this.last_page = res.data.last_page
					uni.hideLoading()
				})
			},
			toForm(type,data){
				uni.navigateTo({
					url:`/pagesMine/address/address_form?type=${type}&form=${type==2 ? JSON.stringify(data) : {}}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.container{
	padding-bottom: calc(constant(safe-area-inset-bottom) + 140rpx);
	padding-bottom: calc(env(safe-area-inset-bottom) + 140rpx);
	.list{
		padding: 0 22rpx;
		box-sizing: border-box;
		.item{
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 12rpx 2rpx rgba(223,223,223,0.16);
			border-radius: 20rpx;
			padding: 20rpx 30rpx 32rpx 20rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 20rpx;
			.left{
				width: calc(100% - 70rpx);
				.city{
					display: flex;
					text{
						display: inline-block;
						width: 92rpx;
						height: 40rpx;
						background: #E72A2A;
						border-radius: 8rpx;
						font-weight: 400;
						font-size: 24rpx;
						color: #FFFFFF;
						line-height: 40rpx;
						text-align: center;
						margin-right: 20rpx;
					}
					.text{
						font-weight: bold;
						font-size: 28rpx;
						color: #000000;
						line-height: 40rpx;
					}
				}
				.info{
					font-weight: 400;
					font-size: 28rpx;
					color: #727272;
					line-height: 40rpx;
					margin-top: 12rpx;
				}
				.user{
					font-weight: bold;
					font-size: 28rpx;
					color: #333333;
					line-height: 40rpx;
				}
			}
			&>image{
				width: 45rpx;
				height: 45rpx;
			}
		}
	}
	
	.add-btn{
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx 22rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
		box-sizing: border-box;
		button{
			width: 100%;
			height: 96rpx;
			background: linear-gradient( 135deg, #FFA233 0%, #FB4C4C 100%);
			border-radius: 80rpx;
			font-weight: bold;
			font-size: 36rpx;
			color: #FFFFFF;
			line-height: 96rpx;
		}
	}
}
</style>
